package com.csweimei.rentmanagementtreasure.tenant.ui.act.dayPaper;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProviders;

import android.os.Bundle;

import com.chad.library.BR;
import com.csweimei.rentmanagementtreasure.R;
import com.csweimei.rentmanagementtreasure.adapter.PostImageAdapter;
import com.csweimei.rentmanagementtreasure.base.AppViewModelFactory;
import com.csweimei.rentmanagementtreasure.databinding.ActivityAddDayPaperBinding;
import com.csweimei.rentmanagementtreasure.tenant.viewModel.AddDPVM;
import com.csweimei.rentmanagementtreasure.utils.GlideEngine;
import com.csweimei.rentmanagementtreasure.utils.permission.RxPermissionsUtils;
import com.luck.picture.lib.basic.PictureSelector;
import com.luck.picture.lib.config.SelectMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.picture.lib.interfaces.OnResultCallbackListener;

import java.util.ArrayList;
import java.util.List;

import me.goldze.mvvmhabit.base.BaseActivity;

public class AddDayPaperActivity extends BaseActivity<ActivityAddDayPaperBinding, AddDPVM> {


    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_add_day_paper;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }


    PostImageAdapter imageAdapter;
    private List<String> mSelected = new ArrayList<>();

    @Override
    public void initData() {
        super.initData();
        viewModel.setActivity(this);


        imageAdapter = new PostImageAdapter(this, 3,
                null, new PostImageAdapter.OnItemClickListener() {
            @Override
            public void OnItemClick(int position) {


            }

            @Override
            public void OnAddItemClick(int count) {
                RxPermissionsUtils.file(AddDayPaperActivity.this, (RxPermissionsUtils.Accept) () -> {
                    PictureSelector.create(AddDayPaperActivity.this)
                            .openGallery(SelectMimeType.ofImage())
                            .isDisplayCamera(false)
                            .setImageEngine(GlideEngine.createGlideEngine())
                            .setMaxSelectNum(3)
                            .setMinSelectNum(1)
                            .setImageSpanCount(4)
                            .forResult(new OnResultCallbackListener<LocalMedia>() {
                                @Override
                                public void onResult(ArrayList<LocalMedia> result) {
                                    mSelected.clear();
                                    for (LocalMedia data : result) {
                                        mSelected.add(data.getRealPath());
                                    }
                                    imageAdapter.AddDatas(mSelected);
                                    viewModel.setImgList(imageAdapter.getDatas());
                                }

                                @Override
                                public void onCancel() {

                                }
                            });
                });
            }

            @Override
            public void OnDelClick(int position) {
                viewModel.setImgList(imageAdapter.getDatas());

            }
        });
        binding.imgRecy.setAdapter(imageAdapter);
    }

    @Override
    public AddDPVM initViewModel() {
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getApplication());
        return ViewModelProviders.of(this, factory).get(AddDPVM.class);
    }
}